﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;

namespace DashFire
{
  class GameConfig
  {
    public static string s_AppName = "df";
    public static string s_GroupName = "dev";
    public static string s_ChannelName = "orange";
    public static string s_NoticeConfigUrl = "http://192.168.1.30/dev/Notice.txt";
    public static string s_ServerListUrl = "http://192.168.1.30/dev/ServerConfig.txt";
    public static string s_ChannelConfigUrl = "http://192.168.1.30/dev/ChannelConfig_ios_orange.txt";
    public static void Load(string iniFile, byte[] buffer)
    {
      ConfigFile m_IniFile = new ConfigFile(iniFile, buffer);
      s_AppName = m_IniFile.GetSetting("Config", "AppName");
      s_GroupName = m_IniFile.GetSetting("Config", "GroupName");
      s_ChannelName = m_IniFile.GetSetting("Config", "ChannelName");
      s_NoticeConfigUrl = m_IniFile.GetSetting("Config", "NoticeConfigUrl");
      s_ServerListUrl = m_IniFile.GetSetting("Config", "ServerListUrl");
      s_ChannelConfigUrl = m_IniFile.GetSetting("Config", "ChannelConfigUrl");
    }
  }
  class GameConfigLoader
  {
    private const string s_GameConfigFile = "GameConfig.txt";
    public static string s_NoticeContent = string.Empty;

    public static ResAsyncInfo RequestGameConfig()
    {
      ResAsyncInfo info = new ResAsyncInfo();
      info.CurCoroutine = CoroutineProxy.Instance.StartCoroutine(RequestGameConfigAsync(info));
      return info;
    }
    private static IEnumerator RequestGameConfigAsync(ResAsyncInfo info)
    {
      string gameConfigUrl = ResLoadHelper.Combine(ResLoadHelper.GetStreamingAssetPath(), s_GameConfigFile);
      ResLoadHelper.Log("RequestGameConfigAsync URL;" + gameConfigUrl);
      using (WWW tWWW = new WWW(gameConfigUrl)) {
        yield return tWWW;
        try {
          if (tWWW.error != null) {
            ResLoadHelper.Log("RequestGameConfigAsync error");
            info.IsError = true;
            ResUpdateControler.s_UpdateError = ResUpdateError.RequestGameConfig_WWW_Error;
            tWWW.Dispose();
            yield break;
          }
          byte[] bytes = tWWW.bytes;
          if (bytes == null || bytes.Length == 0) {
            ResLoadHelper.Log("RequestGameConfigAsync bytes null or empty data;" + gameConfigUrl);
            info.IsError = true;
            ResUpdateControler.s_UpdateError = ResUpdateError.RequestGameConfig_Byte_Error;
            tWWW.Dispose();
            yield break;
          }
          GameConfig.Load(gameConfigUrl, bytes);
        } catch (System.Exception ex) {
          ResLoadHelper.Log("RequestGameConfigAsync ex:" + ex);
          info.IsError = true;
          ResUpdateControler.s_UpdateError = ResUpdateError.RequestGameConfig_Save_Error;
          yield break;
        } finally {
          tWWW.Dispose();
        }
      }
      info.IsDone = true;
      info.Progress = 1.0f;
    }
  }
}
